﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Controls;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Word;

namespace HuiZe.education.View.Ethan
{
    /// <summary>
    /// PART_Media_Office.xaml 的交互逻辑
    /// </summary>
    public partial class PART_Media_Office : UserControl
    {
        public PART_Media_Office()
        {
            InitializeComponent();
            PART_WebBrowser.ScriptErrorsSuppressed = true;
        }

        public void SetUrl(string _url)
        {
            //string _html = $"<iframe data-v-36cdbd10=\"\" src=\"https://ow365.cn/?i=28004&amp;ssl=1&amp;n=5&amp;furl={_url}\" class=\"course-iframe\" style=\"background-color: rgb(255, 255, 255);\"></iframe>";
            string _html = $"<iframe data-v-36cdbd10 src='https://ow365.cn/?i=28004&ssl=1&n=5&furl={_url}' style='background-color: rgb(255, 255, 255);'  width='100%' height='100%' frameborder='0'></iframe>";
            PART_WebBrowser.DocumentText = _html;
        }
        
        public void SetFile(string _path, int _type = 0)
        {
            if (_type == 2)
            {
                PART_WebBrowser.Navigate(_path);
                return;
            }

            string _path_html = Path.ChangeExtension(_path, ".html");
            if (!File.Exists(_path_html))
            {
                switch (_type)
                {
                    case 0:
                        ExcelToHtml(_path, _path_html);
                        break;
                    case 1:
                        WordToHtml(_path, _path_html);
                        break;
                    case 3:
                        PptToHtml(_path, _path_html);
                        break;
                }
            }
            if (File.Exists(_path_html))
            {
                PART_WebBrowser.Navigate(new Uri(_path_html, UriKind.RelativeOrAbsolute));
            }

            //string _html = $"<iframe data-v-5f534b4b='' src='https://ow365.cn/?i=28004&amp;ssl=1&amp;n=5&amp;furl={_path}' class='course-iframe' style='background-color: rgb(255, 255, 255);'  width='100%' height='100%' frameborder='0'></iframe>";
            //_html = $"<iframe src='https://view.officeapps.live.com/op/view.aspx?src={_path}' style='background-color: rgb(255, 255, 255);'  width='100%' height='100%' frameborder='0'></iframe>";
            //PART_WebBrowser.DocumentText = _html;
        }

        public void Reset()
        {
            PART_WebBrowser.Navigate("about:blank");
        }

        void ExcelToHtml(string _path_excel, string _path_html)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application application = null;
                Workbook workbook = null;
                application = new Microsoft.Office.Interop.Excel.Application();
                object missing = Type.Missing;
                object trueObject = true;
                application.Visible = false;
                application.DisplayAlerts = false;
                workbook = application.Workbooks.Open(_path_excel, missing, trueObject, missing, missing, missing,
                   missing, missing, missing, missing, missing, missing, missing, missing, missing);
                object format = XlFileFormat.xlHtml;
                workbook.SaveAs(_path_html, XlFileFormat.xlHtml, null, null, false, false,
                   XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);

                workbook.Close();
                application.Quit();
            }
            catch (Exception ex)
            {
                App.Log(ex);
            }
        }

        void WordToHtml(string _path_word, string _path_html)
        {
            try
            {
                Microsoft.Office.Interop.Word._Application application = null;
                Microsoft.Office.Interop.Word._Document doc = null;
                application = new Microsoft.Office.Interop.Word.Application();
                object missing = Missing.Value;
                object trueObject = true;
                application.Visible = false;
                application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                object physicalPathObj = _path_word;
                doc = application.Documents.Open(ref physicalPathObj, ref missing, ref trueObject, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                object format = WdSaveFormat.wdFormatHTML;
                object _save_html = _path_html;
                doc.SaveAs(ref _save_html, ref format, ref missing, ref missing, ref missing,
                           ref missing, XlSaveAsAccessMode.xlNoChange, ref missing,
                           ref missing, ref missing, ref missing, ref missing);

                doc.Close();
                application.Quit();
            }
            catch (Exception ex)
            {
                App.Log(ex);
            }
        }

        void PptToHtml(string _path_ppt, string _path_html)
        {
            //Spire.Presentation.Presentation ppt = new Spire.Presentation.Presentation();
            try
            {
                //ppt.LoadFromFile(_path_ppt);
                //ppt.SaveToFile(_path_html, FileFormat.Html);
            }
            catch (Exception ex)
            {

            }

            //Microsoft.Office.Interop.PowerPoint.Application _ppt = null;
            //Microsoft.Office.Interop.PowerPoint.Presentation _document = null;

            //try
            //{
            //    _ppt = new Microsoft.Office.Interop.PowerPoint.Application();
            //    _document = _ppt.Presentations.Open(_path_ppt, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

            //    _document.SaveAs(_path_html, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, Microsoft.Office.Core.MsoTriState.msoFalse);

            //}
            //catch (Exception ex)
            //{
            //    App.Log(ex);
            //}
            //finally
            //{
            //    if (_document != null)
            //        _document.Close();
            //    if (_ppt != null)
            //        _ppt.Quit();
            //}
        }
    }
}
